package joyson.email.controller;

import joyson.email.mapper.UserMapper;
import joyson.email.utils.DateTimeUtil;
import joyson.email.vo.response.Result;
import joyson.email.service.PictureService;
import joyson.email.service.SendEmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 邮件发送控制层
 * @Author ke
 * @Date 2024/5/18 13:35
 */
@RestController
@RequestMapping("/email")
public class EmailController {

    @Autowired
    private SendEmailService sendEmailService;

    @Autowired
    private PictureService pictureService;

    @Autowired
    private UserMapper userMapper;

    @GetMapping("/send")
    public Result<Boolean> sendEmail() {
        List<Map<String, Object>> users = userMapper.selectUser(DateTimeUtil.getStartTime(), DateTimeUtil.getEndTime());

        for (Map<String, Object> user : users) {
            String name = (String) user.get("name");
            String email = (String) user.get("email");
            Map<String, String> params = new HashMap<>();
            params.put("name", name);
            params.put("to", email);
            params.put("subject", "祝您生日快乐");
            //制作图片
            pictureService.handleBirthDay(params);
            //发送邮件
            sendEmailService.send(params);
        }
        return Result.success(true, "发送成功");
    }
}
